const {
	Controller
} = require('uni-cloud-router');
const {
	success,
	exception,
	error,
	typeOf,
	StatusEnum
} = require("lg-utils");
module.exports = class CommonController extends Controller {



	/***
		 @type GET 
		 @description : xxx
		 @param {type}
	***/
	async xxx() {
		try {
			const {
				service,
				data = {}
			} = this.ctx;

			const {
				affectedDocs,
				data: list
			} = await service.common.xxx() || {};
			if (typeOf(affectedDocs, StatusEnum.UNDEFINED)) {
				return error("xxx;", StatusEnum.UNDEFINED);
			} else {
				return success(list);
			}

		} catch (e) {
			return exception(this, "xxx;", e);
		}

	}

	/***
		 @type GET 
		 @description : 根据type更新单项数据data(push)
	     @param {
			 type = "",  字典的type
			 data = ""   push的数据项
		 }
	***/
	async updateIterPushDateByType() {
		try {
			const {
				service,
				data = {}
			} = this.ctx;

			return await service.common.updateIterPushDateByType(data) || {};

		} catch (e) {
			return exception(this, "根据type更新单项数据data(push);", e);
		}

	}

	/***
		 @type GET 
		 @description : 根据type更新单项数据data(pop)
	     @param {
			 type = "",  字典的type
		 }
	***/
	async updateIterPopDateByType() {
		try {
			const {
				service,
				data = {}
			} = this.ctx;

			return await service.common.updateIterPopDateByType(data) || {};

		} catch (e) {
			return exception(this, "根据type更新单项数据data(pop);", e);
		}

	}

	/***
		 @type GET 
		 @description : 新增字典数据项
		 @param {
			 iters 插入数据项
		 }
	***/
	async addDictDataItem() {
		try {
			const {
				service,
				data
			} = this.ctx;
			let iters = [];
			if (JSON.stringify(data) == "{}") {
				return error("传入无效的参数;", "{}")
			}
			if (typeOf(data, StatusEnum.ARRAY)) {
				iters = [...data];
			} else if (typeOf(data, StatusEnum.OBJECT)) {
				iters = [data];
			} else {
				iters = null;
			}

			const serviceRes = await service.common.addDictDataItem(iters);
			const {
				inserted
			} = serviceRes || {};
			if (typeOf(inserted, StatusEnum.UNDEFINED)) {
				return serviceRes;
			} else {
				return success(inserted);
			}

		} catch (e) {
			return exception(this, "新增字典数据项异常;", e);
		}

	}
	/***
			 @type GET 
			 @description : 根据查询条件获取数据字典
			 @param {type}
		***/
	async getDictDataByWhere() {
		try {
			const {
				service,
				data = {}
			} = this.ctx;

			const {
				affectedDocs,
				data: list
			} = await service.common.getDictDataByWhere(data) || {};
			if (typeOf(affectedDocs, StatusEnum.UNDEFINED)) {
				return error("根据查询条件获取数据字典service异常;", StatusEnum.UNDEFINED);
			} else {
				return success(list);
			}

		} catch (e) {
			return exception(this, "根据查询条件获取数据字典异常;", e);
		}

	}



	/***
		 @type GET 
		 @description : 根据type拿相对应字典数据
		 @param {
			 type = "" 数据字典type
		 }
	***/
	async getDictDataByType() {
		try {
			const {
				service,
				data = {}
			} = this.ctx;
			const {
				affectedDocs,
				data: list
			} = await service.common.getDictDataByType(data) || {};
			if (typeOf(affectedDocs, StatusEnum.UNDEFINED)) {
				return error("根据type获取对应字典数据service异常;", StatusEnum.UNDEFINED);
			} else {
				return success(list[0].data);
			}
		} catch (e) {
			return exception(this, "根据type获取对应字典数据异常", e);
		}

	}

}
